package com.woniuxy.book.exception.handler;

import com.woniuxy.book.exception.account.AccountException;
import com.woniuxy.book.exception.bookinfo.BookInfoException;
import com.woniuxy.book.exception.booktype.BookTypeException;
import com.woniuxy.book.exception.orderinfo.OrderInfoException;
import com.woniuxy.book.exception.orderrefund.OrderRefundException;
import com.woniuxy.book.exception.useraddress.UserAddressException;
import com.woniuxy.book.exception.userinfo.UserInfoException;
import com.woniuxy.book.util.Result;
import io.jsonwebtoken.ExpiredJwtException;
import io.jsonwebtoken.MalformedJwtException;
import io.jsonwebtoken.security.SignatureException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;


@RestControllerAdvice
public class GlobalExceptionHandler {
    //该方法专用于处理账户操作相关的异常
    @ExceptionHandler(AccountException.class)
    public Result handlerAccountException(AccountException e){
        e.printStackTrace();
        return Result.fail(e.getMessage());
    }

    @ExceptionHandler(MalformedJwtException.class)
    public Result handlerMalformedJwtException(MalformedJwtException e){
        return Result.fail("令牌无效");
    }

    @ExceptionHandler(SignatureException.class)
    public Result handlerSignatureException(SignatureException e){
        return Result.fail("签名异常");
    }

    @ExceptionHandler(ExpiredJwtException.class)
    public Result handlerExpiredJwtException(ExpiredJwtException e){
        return Result.fail("JWT过期");
    }

    @ExceptionHandler(UserInfoException.class)
    public Result handlerUserInfoException(UserInfoException e){
        e.printStackTrace();
        return Result.fail(e.getMessage());
    }

    @ExceptionHandler(BookTypeException.class)
    public Result handlerBookTypeException(BookTypeException e){
        e.printStackTrace();
        return Result.fail(e.getMessage());
    }

    @ExceptionHandler(BookInfoException.class)
    public Result handlerBookInfoException(BookInfoException e){
        e.printStackTrace();
        return Result.fail(e.getMessage());
    }

    @ExceptionHandler(OrderInfoException.class)
    public Result handlerOrderInfoException(OrderInfoException e){
        e.printStackTrace();
        return Result.fail(e.getMessage());
    }

    @ExceptionHandler(OrderRefundException.class)
    public Result handlerOrderRefundException(OrderRefundException e){
        e.printStackTrace();
        return Result.fail(e.getMessage());
    }

    @ExceptionHandler(UserAddressException.class)
    public Result handlerUserAddressException(UserAddressException e){
        e.printStackTrace();
        return Result.fail(e.getMessage());
    }






    //兜底处理异常
    @ExceptionHandler(Exception.class)
    public Result handlerException(Exception e){
        e.printStackTrace();
        return Result.fail(e.getMessage());
    }

}
